home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sfilter1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.2 KB  |  311 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sfilter1.c,v 1.5.2.1 2000/11/02 15:05:07 igorm Exp $ */
  20. /* Filters included in Level 1 systems: NullEncode/Decode, PFBDecode, */
  21. /*   SubFileDecode. */
  22. #include "stdio_.h"        /* includes std.h */
  23. #include "memory_.h"
  24. #include "strimpl.h"
  25. #include "sfilter.h"
  26.  
  27. /* ------ PFBDecode ------ */
  28.  
  29. private_st_PFBD_state();
  30.  
  31. /* Initialize the state */
  32. private int
  33. s_PFBD_init(stream_state * st)
  34. {
  35.     stream_PFBD_state *const ss = (stream_PFBD_state *) st;
  36.  
  37.     ss->record_type = -1;
  38.     return 0;
  39. }
  40.  
  41. /* Process a buffer */
  42. private int
  43. s_PFBD_process(stream_state * st, stream_cursor_read * pr,
  44.            stream_cursor_write * pw, bool last)
  45. {
  46.     stream_PFBD_state *const ss = (stream_PFBD_state *) st;
  47.     register const byte *p = pr->ptr;
  48.     register byte *q = pw->ptr;
  49.     int rcount, wcount;
  50.     int c;
  51.     int status = 0;
  52.  
  53. top:
  54.     rcount = pr->limit - p;
  55.     wcount = pw->limit - q;
  56.     switch (ss->record_type) {
  57.     case -1:        /* new record */
  58.         if (rcount < 2)
  59.         goto out;
  60.         if (p[1] != 0x80)
  61.         goto err;
  62.         c = p[2];
  63.         switch (c) {
  64.         case 1:
  65.         case 2:
  66.             break;
  67.         case 3:
  68.             status = EOFC;
  69.             p += 2;
  70.             goto out;
  71.         default:
  72.             p += 2;
  73.             goto err;
  74.         }
  75.         if (rcount < 6)
  76.         goto out;
  77.         ss->record_type = c;
  78.         ss->record_left = p[3] + ((uint) p[4] << 8) +
  79.         ((ulong) p[5] << 16) +
  80.         ((ulong) p[6] << 24);
  81.         p += 6;
  82.         goto top;
  83.     case 1:        /* text data */
  84.         /* Translate \r to \n. */
  85.         {
  86.         int count = (wcount < rcount ? (status = 1, wcount) : rcount);
  87.  
  88.         if (count > ss->record_left)
  89.             count = ss->record_left,
  90.             status = 0;
  91.         ss->record_left -= count;
  92.         for (; count != 0; count--) {
  93.             c = *++p;
  94.             *++q = (c == '\r' ? '\n' : c);
  95.         }
  96.         }
  97.         break;
  98.     case 2:        /* binary data */
  99.         if (ss->binary_to_hex) {
  100.         /* Translate binary to hex. */
  101.         int count;
  102.         const char *const hex_digits = "0123456789abcdef";
  103.  
  104.         wcount >>= 1;    /* 2 chars per input byte */
  105.         count = (wcount < rcount ? (status = 1, wcount) : rcount);
  106.         if (count > ss->record_left)
  107.             count = ss->record_left,
  108.             status = 0;
  109.         ss->record_left -= count;
  110.         for (; count != 0; count--) {
  111.             c = *++p;
  112.             q[1] = hex_digits[c >> 4];
  113.             q[2] = hex_digits[c & 0xf];
  114.             q += 2;
  115.         }
  116.         } else {        /* Just read binary data. */
  117.         int count = (wcount < rcount ? (status = 1, wcount) : rcount);
  118.  
  119.         if (count > ss->record_left)
  120.             count = ss->record_left,
  121.             status = 0;
  122.         ss->record_left -= count;
  123.         memcpy(q + 1, p + 1, count);
  124.         p += count;
  125.         q += count;
  126.         }
  127.         break;
  128.     }
  129.     if (ss->record_left == 0) {
  130.     ss->record_type = -1;
  131.     goto top;
  132.     }
  133. out:
  134.     pr->ptr = p;
  135.     pw->ptr = q;
  136.     return status;
  137. err:
  138.     pr->ptr = p;
  139.     pw->ptr = q;
  140.     return ERRC;
  141. }
  142.  
  143. /* Stream template */
  144. const stream_template s_PFBD_template = {
  145.     &st_PFBD_state, s_PFBD_init, s_PFBD_process, 6, 2
  146. };
  147.  
  148. /* ------ SubFileDecode ------ */
  149.  
  150. private_st_SFD_state();
  151.  
  152. /* Set default parameter values. */
  153. private void
  154. s_SFD_set_defaults(stream_state * st)
  155. {
  156.     stream_SFD_state *const ss = (stream_SFD_state *) st;
  157.  
  158.     ss->count = 0;
  159.     ss->eod.data = 0;
  160.     ss->eod.size = 0;
  161.     ss->skip_count = 0;
  162. }
  163.  
  164. /* Initialize the stream */
  165. private int
  166. s_SFD_init(stream_state * st)
  167. {
  168.     stream_SFD_state *const ss = (stream_SFD_state *) st;
  169.  
  170.     ss->match = 0;
  171.     ss->copy_count = 0;
  172.     ss->min_left = (ss->eod.size != 0);
  173.  
  174.     return 0;
  175. }
  176.  
  177. /* Refill the buffer */
  178. private int
  179. s_SFD_process(stream_state * st, stream_cursor_read * pr,
  180.           stream_cursor_write * pw, bool last)
  181. {
  182.     stream_SFD_state *const ss = (stream_SFD_state *) st;
  183.     register const byte *p = pr->ptr;
  184.     register byte *q = pw->ptr;
  185.     const byte *rlimit = pr->limit;
  186.     byte *wlimit = pw->limit;
  187.     int status = 0;
  188.  
  189.     if (ss->eod.size == 0) {    /* Just read, with no EOD pattern. */
  190.     int rcount = rlimit - p;
  191.     int wcount = wlimit - q;
  192.     int count;
  193.  
  194.     if (rcount <= ss->skip_count) { /* skipping */
  195.         ss->skip_count -= rcount;
  196.         pr->ptr = rlimit;
  197.         return 0;
  198.     } else if (ss->skip_count > 0) {
  199.         rcount -= ss->skip_count;
  200.         pr->ptr = p += ss->skip_count;
  201.         ss->skip_count = 0;
  202.     }
  203.     count = min(rcount, wcount);
  204.     if (ss->count == 0)    /* no EOD limit */
  205.         return stream_move(pr, pw);
  206.     else if (ss->count > count) {    /* not EOD yet */
  207.         ss->count -= count;
  208.         return stream_move(pr, pw);
  209.     } else {        /* We're going to reach EOD. */
  210.         count = ss->count;
  211.         if (count > 0) {
  212.         memcpy(q + 1, p + 1, count);
  213.         pr->ptr = p + count;
  214.         pw->ptr = q + count;
  215.         }
  216.         ss->count = -1;
  217.         return EOFC;
  218.     }
  219.     } else {            /* Read looking for an EOD pattern. */
  220.     const byte *pattern = ss->eod.data;
  221.     uint match = ss->match;
  222.  
  223. cp:
  224.     /* Check whether we're still copying a partial match. */
  225.     if (ss->copy_count) {
  226.         int count = min(wlimit - q, ss->copy_count);
  227.  
  228.         memcpy(q + 1, ss->eod.data + ss->copy_ptr, count);
  229.         ss->copy_count -= count;
  230.         ss->copy_ptr += count;
  231.         q += count;
  232.         if (ss->copy_count != 0) {    /* hit wlimit */
  233.         status = 1;
  234.         goto xit;
  235.         } else if (ss->count < 0) {
  236.         status = EOFC;
  237.         goto xit;
  238.         }
  239.     }
  240.     while (p < rlimit) {
  241.         int c = *++p;
  242.  
  243.         if (c == pattern[match]) {
  244.         if (++match == ss->eod.size) {
  245.             if (ss->skip_count > 0) {
  246.             q = pw->ptr; /* undo any writes */
  247.             ss->skip_count--;
  248.             match = 0;
  249.             continue;
  250.             }
  251.             /*
  252.              * We use if/else rather than switch because the value
  253.              * is long, which is not supported as a switch value in
  254.              * pre-ANSI C.
  255.              */
  256.             if (ss->count <= 0) {
  257.             status = EOFC;
  258.             goto xit;
  259.             } else if (ss->count == 1) {
  260.             ss->count = -1;
  261.             } else
  262.             ss->count--;
  263.             ss->copy_ptr = 0;
  264.             ss->copy_count = match;
  265.             match = 0;
  266.             goto cp;
  267.         }
  268.         continue;
  269.         }
  270.         /*
  271.          * No match here, back up to find the longest one.
  272.          * This may be quadratic in string_size, but
  273.          * we don't expect this to be a real problem.
  274.          */
  275.         if (match > 0) {
  276.         int end = match;
  277.  
  278.         while (match > 0) {
  279.             match--;
  280.             if (!memcmp(pattern, pattern + end - match, match))
  281.             break;
  282.         }
  283.         /*
  284.          * Copy the unmatched initial portion of
  285.          * the EOD string to the output.
  286.          */
  287.         p--;
  288.         ss->copy_ptr = 0;
  289.         ss->copy_count = end - match;
  290.         goto cp;
  291.         }
  292.         if (q == wlimit) {
  293.         p--;
  294.         status = 1;
  295.         break;
  296.         }
  297.         *++q = c;
  298.     }
  299. xit:    pr->ptr = p;
  300.     if (ss->skip_count <= 0)
  301.         pw->ptr = q;
  302.     ss->match = match;
  303.     }
  304.     return status;
  305. }
  306.  
  307. /* Stream template */
  308. const stream_template s_SFD_template = {
  309.     &st_SFD_state, s_SFD_init, s_SFD_process, 1, 1, 0, s_SFD_set_defaults
  310. };
  311.